package com.panda.mall.elastic.config;

import com.panda.mall.common.constants.ConfigConstants;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;

/**
 * author: tcy
 * createDate: 2023/3/2
 * description:
 */
@Configuration
public class ElasticConfiguration {

    @Bean
    public RestHighLevelClient restHighLevelClient(){

        RestClientBuilder builder = RestClient.builder(new HttpHost(ConfigConstants.HTTP_host, ConfigConstants.ES_port, ConfigConstants.ES_scheme))
                .setRequestConfigCallback(config-> config.setConnectTimeout(5000).setConnectionRequestTimeout(30000).setSocketTimeout(30000));

        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);

        return ObjectUtils.isEmpty(restHighLevelClient) ? null : restHighLevelClient;
    }

}
